package cn.iocoder.yudao.module.station.service.cylinderblacklist;

import cn.iocoder.yudao.module.station.dal.dataobject.cylinder.CylinderDO;
import cn.iocoder.yudao.module.station.dal.mysql.cylinder.CylinderMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.station.controller.admin.cylinderblacklist.vo.*;
import cn.iocoder.yudao.module.station.dal.dataobject.cylinderblacklist.CylinderBlacklistDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.station.dal.mysql.cylinderblacklist.CylinderBlacklistMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.station.enums.ErrorCodeConstants.*;

/**
 * 钢瓶黑名单 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CylinderBlacklistServiceImpl implements CylinderBlacklistService {

    @Resource
    private CylinderBlacklistMapper cylinderBlacklistMapper;

    @Resource
    private CylinderMapper cylinderMapper;

    @Override
    public Integer createCylinderBlacklist(CylinderBlacklistSaveReqVO createReqVO) {
        String cylinderCode = createReqVO.getCylinderCode();
        Long count = cylinderBlacklistMapper.getCountByCylinderCode(cylinderCode);
        if(count>0){
            throw exception(CYLINDER_BLACKLIST_EXISTS);
        }
        Long selectCount = cylinderMapper.selectCount(CylinderDO::getCylinderCode, cylinderCode);
        if(selectCount==0){
            throw exception(CYLINDER_NOT_EXISTS);
        }else if(selectCount>1){
            throw exception(CYLINDER_DUPLICATE_EXISTS);
        }
        CylinderDO selectOne = cylinderMapper.selectOne(CylinderDO::getCylinderCode, cylinderCode);
        if(selectOne!=null){
            createReqVO.setChipCode(selectOne.getChipCode());
            createReqVO.setRecordUser(selectOne.getCreator());
            createReqVO.setRecordStation(String.valueOf(selectOne.getStationId()));
            createReqVO.setMakeTime(selectOne.getMakeTime());
            createReqVO.setLastCheckTime(selectOne.getRecentCheckTime());
            createReqVO.setNextCheckTime(selectOne.getNextCheckTime());
            createReqVO.setGasCylinderSpeci(selectOne.getGasCylinderSpeci());
            createReqVO.setCylinderStatus(selectOne.getCylinderStatus());
            createReqVO.setInterceptTime(LocalDateTime.now());
        }
        // 插入
        CylinderBlacklistDO cylinderBlacklist = BeanUtils.toBean(createReqVO, CylinderBlacklistDO.class);
        cylinderBlacklistMapper.insert(cylinderBlacklist);
        // 返回
        return cylinderBlacklist.getId();
    }

    @Override
    public void updateCylinderBlacklist(CylinderBlacklistSaveReqVO updateReqVO) {
        // 校验存在
        validateCylinderBlacklistExists(updateReqVO.getId());
        // 更新
        CylinderBlacklistDO updateObj = BeanUtils.toBean(updateReqVO, CylinderBlacklistDO.class);
        cylinderBlacklistMapper.updateById(updateObj);
    }

    @Override
    public void deleteCylinderBlacklist(Integer id) {
        // 校验存在
        validateCylinderBlacklistExists(id);
        // 删除
        cylinderBlacklistMapper.deleteById(id);
    }

    private void validateCylinderBlacklistExists(Integer id) {
        if (cylinderBlacklistMapper.selectById(id) == null) {
            throw exception(CYLINDER_BLACKLIST_NOT_EXISTS);
        }
    }

    @Override
    public CylinderBlacklistDO getCylinderBlacklist(Integer id) {
        return cylinderBlacklistMapper.selectById(id);
    }

    @Override
    public PageResult<CylinderBlacklistDO> getCylinderBlacklistPage(CylinderBlacklistPageReqVO pageReqVO) {
        return cylinderBlacklistMapper.selectPage(pageReqVO);
    }

    @Override
    public Long getCountByCylinderCode(String cylinderCode) {
        return cylinderBlacklistMapper.getCountByCylinderCode(cylinderCode);
    }

}